<script lang="ts" setup>
import { message } from '@/components/XtxUI';
import { counterClock } from '@/hooks';
import { reactive, ref, watch } from 'vue';
import useStore from '@/store';
const prop = defineProps<{
  uuid: string;
}>();
const member = useStore().member;
const form = reactive({
  account: '',
  mobile: '',
  code: '',
  password: '',
});
const isShowCode = ref(false);
const password2 = ref('');
const { start, pause, resume, count } = counterClock(10);

// 通过监视count值变化，改变验证码的切换
watch(
  count,
  (newValue, oldValue) => {
    if (newValue <= 0) {
      isShowCode.value = false;
    }
  },
  { immediate: true }
);
// 发送验证码
const phoneRgx = /^1[3-9]\d{9}$/;
function sendCode() {
  if (!phoneRgx.test(form.mobile)) {
    return message({
      type: 'error',
      text: '手机号格式错误，无法发送验证码',
    });
  }
  message({
    type: 'success',
    text: '可以发送验证码',
  });
  isShowCode.value = true;
  if (isShowCode.value) {
    // 调用倒计时hook
    start();
  }
}

//点击提交进行校验
function checkForm() {
  if (!form.account) {
    return message({
      type: 'error',
      text: '账户为空',
    });
  }
  if (!form.mobile) {
    return message({
      type: 'error',
      text: '手机为空',
    });
  }
  if (!phoneRgx.test(form.mobile)) {
    return message({
      type: 'error',
      text: '手机号格式错误，无法发送验证码',
    });
  }
  if (!form.code) {
    return message({
      type: 'error',
      text: '验证码为空',
    });
  }
  if (!form.password) {
    return message({
      type: 'error',
      text: '密码为空',
    });
  }
  if (form.password != password2.value) {
    return message({
      type: 'error',
      text: '输入密码不一致',
    });
  }

  // 简单校验成功后，发送请求注册。
  member.registerLogin(form, prop.uuid);
}
</script>
<template>
  <div>
    <div class="bind">
      <div class="field">
        <i class="icon iconfont icon-user"></i>
        <input
          v-model="form.account"
          maxlength="11"
          class="input"
          type="text"
          placeholder="请输入用户名"
        />
      </div>
      <div class="field">
        <i class="icon iconfont icon-phone"></i>
        <input
          v-model="form.mobile"
          maxlength="11"
          class="input"
          type="text"
          placeholder="绑定的手机号"
        />
      </div>
      <div class="field">
        <i class="icon iconfont icon-code"></i>
        <input
          v-model="form.code"
          maxlength="11"
          class="input"
          type="text"
          placeholder="短信验证码"
        />
        <span class="code" v-if="!isShowCode" @click="sendCode"
          >发送验证码</span
        >
        <span class="code" v-else>{{ count }}秒后再发送验证码</span>
      </div>
      <div class="field">
        <i class="icon iconfont icon-lock"></i>
        <input
          v-model="form.password"
          maxlength="11"
          class="input"
          type="text"
          placeholder="请输入密码"
        />
      </div>
      <div class="field">
        <i class="icon iconfont icon-lock"></i>
        <input
          v-model="password2"
          maxlength="11"
          class="input"
          type="text"
          placeholder="请再次输入密码"
        />
      </div>
      <myButton type="info" size="large" @click="checkForm">立即提交</myButton>
    </div>
  </div>
</template>
<style lang="less" scoped>
.iconfont {
  width: 40px;
  height: 50px;
  position: absolute;
  line-height: 50px;
  left: 0px;
}
.bind {
  text-align: center;
}
.field {
  position: relative;
  margin: 0 auto;
  width: 320px;
  height: 50px;
  text-align: center;
  margin-bottom: 20px;
}
.input {
  border: 1px solid #e4e4e4;
  width: 320px;
  height: 50px;
  line-height: 50px;
  padding: 0 10px 0 40px;
}
.code {
  position: absolute;
  right: 10px;
  line-height: 50px;
  color: #999;
  cursor: pointer;
}
</style>
